package org.itboys.kitchen.controller;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.entity.mysql.order.KitchenOrder;
import org.itboys.kitchen.entity.mysql.order.KitchenOrderProduct;
import org.itboys.kitchen.service.mysql.order.KitchenOrderService;
import org.itboys.kitchen.service.mysql.order.KitchenOrderAccountService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017年5月8日
 * 描述：花果山(农厨)__订单_controller_web
 */
@Controller
@RequestMapping("/kitchen/web/order")
public class KitchenOrderController extends BaseController{
    @Autowired
    private KitchenOrderService orderService;
    @Autowired
    private KitchenOrderAccountService orderAccountService;

    /**
     * 订单模块主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model,
            @RequestParam(value="returnPage",required=true) Integer returnPage){
        if(returnPage==1){
            return  "/kitchen/order/mall/index";
        }else if(returnPage==2){
            return  "/kitchen/order/score/index";
        }else if(returnPage==3){
            return  "/kitchen/order/caigou/index";
        }
        return "";
    }

    /**
     * 查看页面
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true) Long id,
            @RequestParam(value="returnPage",required=true) Integer returnPage,
            Model model
            ){
          KitchenOrder order = orderService.findOne(id,false);
          model.addAttribute("order",order);

        if(returnPage==1){
            return  "/kitchen/order/mall/input";
        }else if(returnPage==2){
            return  "/kitchen/order/score/input";
        }else if(returnPage==3){
            return  "/kitchen/order/caigou/input";
        }
        return"";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response,
                       @RequestParam(value="type",required=true) Integer type
    ) {
        try {

            PageResult<KitchenOrder> result = orderService.selectOrder(request,type);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }

    /**
     * 查询订单详情（f返回数据）
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="orderId",required=true) Long orderId,
                        @RequestParam(value="isPackageProduct",required=false) boolean isPackageProduct
    ) {
        try {

            KitchenOrder result = orderService.findOne(orderId,isPackageProduct);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(result,CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/findOne------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
    /**
     * 分页获取订单商品
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="orderId",required=true) Long orderId
    ) {
        try {

            PageResult<KitchenOrderProduct> result = orderService.selectProduct(request,orderId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
    /**
     * 采购订单发货
     * @param request
     * @param response
     * @param id

     */
    @RequestMapping("/caigou/send")
    public void caigou_send(HttpServletRequest request,
                     HttpServletResponse response,
                            @RequestParam(value="id",required=true) Long id,
                            @RequestParam(value="driverName",required=false)String driverName,
                            @RequestParam(value="driverMobile",required=false) String driverMobile,
                            @RequestParam(value="driverCarNo",required=false) String driverCarNo,
                            @RequestParam(value="driverImageFile",required=false) MultipartFile driverImageFile
    ) {
        try {
            orderService.sendOrder_caigou(id,driverName,driverMobile,driverCarNo,driverImageFile);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/village/caigou/send------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }
    /**
     * 积分订单发货
     * @param request
     * @param response
     * @param id

     */
    @RequestMapping("/score/send")
    public void score_send(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value="id",required=true) Long id,
                            @RequestParam(value="driverName",required=false)String driverName,
                            @RequestParam(value="driverMobile",required=false) String driverMobile,
                            @RequestParam(value="driverCarNo",required=false) String driverCarNo
    ) {
        try {
            orderService.sendOrder_score(id,driverName,driverMobile,driverCarNo);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/village/score/send------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }

    /**
     * 采购-订单-核销首页
     * @param request

     * @param model
     * @return
     */

    @RequestMapping("/account/index")
    public String caigou_account_index(
            HttpServletRequest request,
            Model model){

            return  "/kitchen/order/account/index";

    }

    /**
     *  采购订单核销
     * @param request
     * @param response
     * @param orderIds
     * @param accountRemark

   @RequestMapping("/account")
    public void account(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value="orderIds",required=true) List<Long> orderIds,
                               @RequestParam(value="accountRemark",required=false) String accountRemark
    ) {
        try {
            orderAccountService.account(orderIds,accountRemark);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/account------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }*/
    /**
     * 核销采购订单
     * @param request
     * @param response
     * @param orderId
     * @param accountAmount
     * @param accountRemark
     */
    /*@RequestMapping("/account")
    public void account(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="orderId",required=true) Long orderId,
                        @RequestParam(value="accountAmount",required=true) BigDecimal accountAmount,
                        @RequestParam(value="accountRemark",required=false) String accountRemark
    ) {
        try {
            orderAccountService.account(orderId,accountAmount,accountRemark);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/account------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }*/


    /**核销采购订单
     *
     * @param request
     * @param response
     * @param orderId
     * @param orderProductIds
     * @param accountPrices
     * @param accountNums
     * @param accountRemark
     */
    @RequestMapping("/account")
    public void account(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="orderId",required=true) Long orderId,
                        @RequestParam(value="orderProductIds",required=true) List<Long> orderProductIds,
                        @RequestParam(value="accountPrices",required=true) List<BigDecimal> accountPrices,
                        @RequestParam(value="accountNums",required=true) List<Integer> accountNums,
                        @RequestParam(value="accountRemark",required=false) String accountRemark
    ) {
        try {
           orderAccountService.account(orderId,orderProductIds,accountPrices,accountNums,accountRemark);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/account------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }
    /**
     * 查询采购订单中核销订单
     * @param request
     * @param response
     */
    @RequestMapping("/account/select")
    public void account_select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {

            PageResult<KitchenOrder> result = orderAccountService.selectOrder(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/order/account/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
}
